package com.github.llyb120.qiuya.util;

import java.nio.charset.StandardCharsets;

public final class Convert {

//    private static byte[] longBytes = new byte[Long.BYTES];
//    private static byte[] intBytes = new byte[Integer.BYTES];

    /**
     * int到byte[]
     * @param i
     * @return
     */
    public static byte[] int2byte(int i) {
        return int2byte(i, new byte[Integer.MAX_VALUE], 0);
    }

    public static byte[] int2byte(int i, byte[] result, int start) {
        //由高位到低位
        result[start + 0] = (byte)((i >> 24) & 0xFF);
        result[start + 1] = (byte)((i >> 16) & 0xFF);
        result[start + 2] = (byte)((i >> 8) & 0xFF);
        result[start + 3] = (byte)(i & 0xFF);
        return result;
    }

    /**
     * byte[]转int
     * @param bytes
     * @return
     */
    public static int byte2int(byte[] bytes) {
        return byte2int(bytes, 0);
    }

    public static int byte2int(byte[] bytes, int start) {
        int value= 0;
        //由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift= (4 - 1 - i) * 8;
            value +=(bytes[start + i] & 0x000000FF) << shift;//往高位游
        }
        return value;
    }


    /**
     * 将64位的long值放到8字节的byte数组
     * @param num
     * @return 返回转换后的byte数组
     */
    public static byte[] longToByteArray(long num) {
        return longToByteArray(num, new byte[Long.BYTES], 0);
    }

    public static byte[] longToByteArray(long num, byte[] bytes, int start) {
        byte[] result = bytes;//longBytes;//new byte[8];
        result[start + 0] = (byte) (num >>> 56);// 取最高8位放到0下标
        result[start + 1] = (byte) (num >>> 48);// 取最高8位放到0下标
        result[start + 2] = (byte) (num >>> 40);// 取最高8位放到0下标
        result[start + 3] = (byte) (num >>> 32);// 取最高8位放到0下标
        result[start + 4] = (byte) (num >>> 24);// 取最高8位放到0下标
        result[start + 5] = (byte) (num >>> 16);// 取次高8为放到1下标
        result[start + 6] = (byte) (num >>> 8); // 取次低8位放到2下标
        result[start + 7] = (byte) (num); // 取最低8位放到3下标
        return result;
    }

    /**
     * 将8字节的byte数组转成一个long值
     * @param byteArray
     * @return 转换后的long型数值
     */
    public static long byte2long(byte[] byteArray) {
        return byte2long(byteArray, 0);
    }

    public static long byte2long(byte[] byteArray, int start) {
//        byte[] a = longBytes;//new byte[8];
//        int i = a.length - 1, j = byteArray.length - start - 1;
//        for (; i >= 0; i--, j--) {// 从b的尾部(即int值的低位)开始copy数据
//            if (j >= 0)
//                a[i] = byteArray[j];
//            else
//                a[i] = 0;// 如果b.length不足4,则将高位补0
//        }
//        // 注意此处和byte数组转换成int的区别在于，下面的转换中要将先将数组中的元素转换成long型再做移位操作，
//        // 若直接做位移操作将得不到正确结果，因为Java默认操作数字时，若不加声明会将数字作为int型来对待，此处必须注意。
//        long v0 = (long) (a[0] & 0xff) << 56;// &0xff将byte值无差异转成int,避免Java自动类型提升后,会保留高位的符号位
//        long v1 = (long) (a[1] & 0xff) << 48;
//        long v2 = (long) (a[2] & 0xff) << 40;
//        long v3 = (long) (a[3] & 0xff) << 32;
//        long v4 = (long) (a[4] & 0xff) << 24;
//        long v5 = (long) (a[5] & 0xff) << 16;
//        long v6 = (long) (a[6] & 0xff) << 8;
//        long v7 = (long) (a[7] & 0xff);
//        return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7;
        long value= 0;
        //由高位到低位
        for (int i = 0; i < Long.BYTES; i++) {
            int shift= (Long.BYTES - 1 - i) * 8;
            value +=(byteArray[start + i] & 0xff) << shift;//往高位游
        }
        return value;
    }


    public static byte[] short2byte(short s){
        return short2byte(s, new byte[Short.BYTES], 0);
    }

    public static byte[] short2byte(short s, byte[] bs, int start){
        byte[] b = new byte[2];
        for(int i = 0; i < 2; i++){
            int offset = 16 - (i+1)*8; //因为byte占4个字节，所以要计算偏移量
            b[start + i] = (byte)((s >> offset)&0xff); //把16位分为2个8位进行分别存储
        }
        return b;
    }

    public static short byte2short(byte[] b, int start){
        short l = 0;
        for (int i = 0; i < 2; i++) {
            l<<=8; //<<=和我们的 +=是一样的，意思就是 l = l << 8
            l |= (b[start + i] & 0xff); //和上面也是一样的  l = l | (b[i]&0xff)
        }
        return l;
    }

    public static short byte2short(byte[] b){
        return byte2short(b, 0);
    }

    public static String byte2String(byte[] bs, int start, int len){
        return new String(bs, start, len, StandardCharsets.UTF_8);
    }

//    public static byte[] byte2String(String str, byte[] bs, int start){
//        System.arraycopy(str.getBytes(StandardCharsets.UTF_8), );
//        return bs;
//    }

    public static byte[] byte2byte(byte[] src, byte[] target, int start){
            System.arraycopy(src, 0, target, start, src.length);
            return target;
    }


}
